<?php
/**
 * Class that operate on table 'organizations'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class OrganizationsMySqlDAO implements OrganizationsDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return OrganizationsMySql 
	 */
	public function load($organization){
		$sql = 'SELECT * FROM organizations WHERE organization_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($organization->organization_id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAllsearchby($organization){
		//print_r('hello');exit;
		$sql = 'SELECT * FROM organizations WHERE '.$organization->search_by.' like "%'.$organization->search_by_value.'%" order by '.$organization->order_by.' '.$organization->sort_order.' limit '.$organization->results_per_page.' offset '.$organization->current_page.'';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	public function searchby_count($organization){
		//print_r($organization);exit;
		//print_r("hello");exit;
		$sql = 'SELECT count(*) as total FROM organizations WHERE '.$organization->search_by.' like "%'.$organization->search_by_value.'%"';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		//print_r($this->getCount($sqlQuery));exit;
		return $this->querySingleResult($sqlQuery);
	}
	
	public function queryAll($organization){
		//print_r('hello');exit;
		$sql = 'SELECT * FROM organizations order by '.$organization->order_by.' '.$organization->sort_order.' limit '.$organization->results_per_page.' offset '.$organization->current_page.'';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	public function queryAllCount($organization){
		//print_r($organization);exit;
		//print_r("hello");exit;
		$sql = 'SELECT count(*) as total FROM organizations order by '.$organization->order_by.' '.$organization->sort_order.' limit '.$organization->results_per_page.' offset '.$organization->current_page.'';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		//print_r($this->getCount($sqlQuery));exit;
		return $this->querySingleResult($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM organizations ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param organization primary key
 	 */
	public function delete($organization_id){
		$sql = 'DELETE FROM organizations WHERE organization_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($organization_id);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param OrganizationsMySql organization
 	 */
	public function insert($organization){

		$sql = 'INSERT INTO organizations (organization_name, organization_url, status, created_date, updated_date) VALUES (?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($organization->organization_name);
		$sqlQuery->set($organization->organization_url);
		$sqlQuery->setNumber($organization->status);
		$sqlQuery->set($organization->created_date);
		$sqlQuery->set($organization->updated_date);

		$id = $this->executeInsert($sqlQuery);	
		$organization->organization_id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param OrganizationsMySql organization
 	 */
	public function update($organization){
		$sql = 'UPDATE organizations SET organization_name = ?, organization_url = ?, status = ?, created_date = ?, updated_date = ? WHERE organization_id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($organization->organization_name);
		$sqlQuery->set($organization->organization_url);
		$sqlQuery->setNumber($organization->status);
		$sqlQuery->set($organization->created_date);
		$sqlQuery->set($organization->updated_date);

		$sqlQuery->setNumber($organization->organization_id);
		return $this->executeUpdate($sqlQuery);
	}
	public function update_status($organization){
		$sql = 'UPDATE organizations SET status = ? WHERE organization_id = ?';
		$sqlQuery = new SqlQuery($sql);
		//print_r($sql);exit;
		$sqlQuery->setNumber($organization->status);
		
		$sqlQuery->setNumber($organization->organization_id);
		return $this->executeUpdate($sqlQuery);
	}
	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM organizations';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByOrganizationName($value){
		$sql = 'SELECT * FROM organizations WHERE organization_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByOrganizationUrl($value){
		$sql = 'SELECT * FROM organizations WHERE organization_url = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM organizations WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM organizations WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM organizations WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByOrganizationName($value){
		$sql = 'DELETE FROM organizations WHERE organization_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByOrganizationUrl($value){
		$sql = 'DELETE FROM organizations WHERE organization_url = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM organizations WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM organizations WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM organizations WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return OrganizationsMySql 
	 */
	protected function readRow($row){
		$organization = new Organization();
		
		$organization->organization_id = $row['organization_id'];
		$organization->organization_name = $row['organization_name'];
		$organization->organization_url = $row['organization_url'];
		$organization->status = $row['status'];
		$organization->created_date = $row['created_date'];
		$organization->updated_date = $row['updated_date'];

		return $organization;
	}
	protected function getCount($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readTotal($tab[$i]);
		}
		return $ret;
	}
	protected function readTotal($row){
		$organization = new Organization();
		//return $row['total'];
		$organization->total = $row['0'];
		return $organization;
	}
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return OrganizationsMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>